home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  7.9 KB  |  229 lines

  1. /* cat > headers/error.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* error.h: header for error.c file            */
  4. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  5. /* SCCS information: %W%    %G% - NCSA */
  6.  
  7. #define    error_h        1
  8.  
  9. #include "all.h"
  10. #include "newext.h"
  11.  
  12. /* EOF */
  13. /* cat > src+obj/error/msg2_stderr.c << "EOF" */
  14. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  15. /* msg2_stderr: two-line error message on stderr.    */
  16. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  17. /* SCCS information: %W%    %G% - NCSA */
  18.  
  19. /* #include "error.h" */
  20.  
  21. void msg2_stderr (s1, s2) char *s1, *s2;
  22. {
  23.  
  24.     fprintf (stderr, "%s\n%s\n%s\n%s\n",
  25.          "           ----- ERROR MESSAGE -----",
  26.          s1, s2,
  27.          "           ----- ERROR MESSAGE -----");
  28.     return;
  29. }
  30. /* EOF */
  31.  
  32. /* cat > error/msg_write.c << "EOF" */
  33. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  34. /* msg_write: write message to text subwindow.         */
  35. /*          Complete message handler is here.        */
  36. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  37. /* SCCS information: %W%    %G% - NCSA */
  38.  
  39. /* #include "error.h" */
  40.  
  41. msg_write(msg)
  42. char *msg;
  43. {
  44. /* Implementation note(s):
  45.     1. On the first time through determine the message limit.
  46.     2. The message limit is a user specified default. The current range
  47.        is [3, 100].
  48.     3. A rollaflex of messages is maintained for the user. It
  49.        starts by saving all the messages up to the message limit
  50.        each prepended by a message number of the form
  51.         [msg %d] ->
  52.        When the message limit is first succeeded a restart message
  53.        is outputted followed by the new message starting at 1. The
  54.        original message at the beginning numbered 1 is deleted. This
  55.        cycles indefinitely.
  56.     4. If the message has a number of lines (each separated by a newline)
  57.        each line is output as a new line in the window shifted over by
  58.        the space taken up by the prepended message number. A newline
  59.        is not required to terminate the message. If it is not there
  60.        it is outputted by this function.
  61.     5. The window has been setup to wrap on a character.
  62.     6. The beginning of the last message is always (should be) visible in
  63.        the window.
  64.     7. Currently at most 3 lines can be displayed in the text subwindow.
  65.  
  66.     Sun BUG: textsw_normalize_view () does not work properly. I cannot
  67.          put the the first character of the last message at the TOP
  68.          of the window. It is visible, but not much else.
  69. */
  70.     int abs_msg_limit, i, j, value;
  71.     char prefix[24];    /* include space for 12 digits */
  72.     char prefix_blanks[24];    /* include space for 12 digits */
  73.     char int_scratch1[13];    /* maximum of 12 digits */
  74.     char int_scratch2[13];  /* maximum of 12 digist */
  75.     char *p_defaultstr, *ptr, *msg_remain, *p_line;
  76.     Textsw_index msgsw_ipoint, start, end;
  77.  
  78.     static int startup = 1;
  79.     static int msg_num = 0;
  80.     static int full = 0;
  81.     static int len_prefix, msg_limit;
  82.     static char *format = "[msg %d]           "; /* maximum of 12 digits */
  83.     static char *msg_txt = "[msg ]";
  84.     static char *err_msg1_p1 = "Warning: No defaults message limit - ";
  85.     static char *err_msg1_p2 = " used.\n";
  86.     static char *err_msg2_p1 = "Warning: Bad defaults message limit - ";
  87.     static char *err_msg2_p2 = " used.\n";
  88.     static char *err_msg3_p1 = "Warning: Message limit not in range [3, ";
  89.     static char *err_msg3_p2 = " ] - ";
  90.     static char *err_msg3_p3 = " used.\n";
  91.     static char *restart_msg = "~~~~~~~~~~ Restart Message Numbering ~~~~~~~~~~\n";
  92.  
  93.     msgsw_ipoint = (Textsw_index) window_get (msgsw,
  94.                           TEXTSW_INSERTION_POINT);
  95.     if (msgsw_ipoint == 0)     /* want to continue even though you ask
  96.                    to exit */
  97.     {
  98.         msg_num = 0;
  99.         full = 0;
  100.     }
  101.     if (startup)    /* first time through - setup */
  102.     {
  103.         startup = 0;
  104.         if ((p_defaultstr = get_defaults (MESSAGE_LIMIT)) == NULL)
  105.         {
  106.             msg_limit = DEFAULT_MESSAGE_LIMIT;
  107.             (void) sprintf (int_scratch1, "%d", msg_limit);
  108.             len_prefix = strlen (msg_txt) + strlen (int_scratch1);
  109.             sprintf (prefix, format, ++msg_num);
  110.             (void) strcpy (prefix + len_prefix, " -> ");
  111.             (void) textsw_insert (msgsw, prefix, strlen(prefix));
  112.             (void) textsw_insert (msgsw, err_msg1_p1, strlen (err_msg1_p1));
  113.             (void) textsw_insert (msgsw, int_scratch1, strlen (int_scratch1));
  114.             (void) textsw_insert (msgsw, err_msg1_p2, strlen (err_msg1_p2));
  115.         }
  116.         else
  117.         {
  118.             abs_msg_limit = ABSOLUTE_MESSAGE_LIMIT; 
  119.             msg_limit = strtol (p_defaultstr, &ptr, 10);
  120.             if (*ptr != NULL)    /* bad integer */
  121.             {
  122.                 msg_limit = DEFAULT_MESSAGE_LIMIT;
  123.                 (void) sprintf (int_scratch1, "%d", msg_limit);
  124.                 len_prefix = strlen (msg_txt) + strlen (int_scratch1);
  125.                 sprintf (prefix, format, ++msg_num);
  126.                 (void) strcpy (prefix + len_prefix, " -> ");
  127.                 (void) textsw_insert (msgsw, prefix, strlen(prefix));
  128.                 (void) textsw_insert (msgsw, err_msg2_p1, strlen (err_msg1_p1));
  129.                 (void) textsw_insert (msgsw, int_scratch1, strlen (int_scratch1));
  130.                 (void) textsw_insert (msgsw, err_msg2_p2, strlen (err_msg1_p2));
  131.             }
  132.             else if (msg_limit < 3 || msg_limit > abs_msg_limit) /* bad range */
  133.             {
  134.                 msg_limit = DEFAULT_MESSAGE_LIMIT;
  135.                 (void) sprintf (int_scratch1, "%d", msg_limit);
  136.                 len_prefix = strlen (msg_txt) + strlen (int_scratch1);
  137.                 sprintf (prefix, format, ++msg_num);
  138.                 (void) strcpy (prefix + len_prefix, " -> ");
  139.                 (void) textsw_insert (msgsw, prefix, strlen(prefix));
  140.                 (void) textsw_insert (msgsw, err_msg3_p1, strlen (err_msg1_p1));
  141.                 (void) textsw_insert (msgsw, int_scratch1, strlen (int_scratch1));
  142.                 (void) textsw_insert (msgsw, err_msg3_p2, strlen (err_msg1_p2));
  143.                 (void) sprintf (int_scratch2, "%d", abs_msg_limit);
  144.                 (void) textsw_insert (msgsw, int_scratch2, strlen (int_scratch2));
  145.                 (void) textsw_insert (msgsw, err_msg3_p3, strlen (err_msg1_p2));
  146.             }
  147.             else
  148.             {
  149.                 char tmp[MAXNAMELEN]; /* need space simply to get the length */
  150.  
  151.                 strcpy (tmp, p_defaultstr);
  152.                 (void) strip_wspace (tmp);
  153.                 len_prefix = strlen (msg_txt) + strlen (tmp);
  154.             }
  155.         }
  156.     }
  157.         /* implement the circular list of messages */
  158.     if (msg_num == msg_limit)
  159.     {
  160.         msg_num = 0;
  161.         full = 1;
  162.         (void) textsw_insert (msgsw, restart_msg, strlen(restart_msg));
  163.     }
  164.     if (full)
  165.     {
  166.             /* determine start of string to erase up to from
  167.                the beginning */
  168.         sprintf (prefix, format, (msg_num + 2) % msg_limit);
  169.         (void) strcpy (prefix + len_prefix, " -> ");
  170.         start = 0;
  171.  
  172.             /* 0 means forward search - should always succeed - returns the updated value of start and end */
  173.         value = textsw_find_bytes (msgsw, &start, &end, prefix, strlen (prefix), 0);
  174.  
  175.             /* erase the message - must be able to write into it! */
  176.         window_set (msgsw, TEXTSW_READ_ONLY, FALSE, 0);
  177.         value = textsw_erase (msgsw, 0, start);
  178.         window_set (msgsw, TEXTSW_READ_ONLY, TRUE, 0);
  179.     }
  180.     
  181.     msgsw_ipoint = (Textsw_index) window_get (msgsw,
  182.                           TEXTSW_INSERTION_POINT);
  183.     sprintf (prefix, format, ++msg_num);
  184.     (void) strcpy (prefix + len_prefix, " -> ");
  185.         /* loop for multiple lines (separated by newline) in the 
  186.            string */
  187.     msg_remain = msg;
  188.     for (i = 0; (p_line = strchr (msg_remain, '\n')) != NULL; i++)
  189.     {
  190.         *p_line = '\0';
  191.         if (i == 0)
  192.             (void) textsw_insert (msgsw, prefix, strlen(prefix));
  193.         else if (i == 1)
  194.         {
  195.             for (j = 0; j < strlen(prefix); j++)
  196.                 prefix_blanks[j] = ' ';
  197.             prefix_blanks[strlen(prefix)] = '\0';
  198.             (void) textsw_insert (msgsw, prefix_blanks, strlen(prefix_blanks));
  199.         }
  200.         else
  201.             (void) textsw_insert (msgsw, prefix_blanks, strlen(prefix_blanks));
  202.         (void) textsw_insert (msgsw, msg_remain, strlen(msg_remain));
  203.         (void) textsw_insert (msgsw, "\n", 1);
  204.         msg_remain = p_line + 1;
  205.         *p_line = '\n';
  206.     }
  207.     if (msg[strlen(msg)] != '\n') /* no terminating newline */
  208.     {
  209.         if (i == 0)
  210.             (void) textsw_insert (msgsw, prefix, strlen(prefix));
  211.         else if (i == 1)
  212.         {
  213.             for (j = 0; j < strlen(prefix); j++)
  214.                 prefix_blanks[j] = ' ';
  215.             prefix_blanks[strlen(prefix)] = '\0';
  216.             (void) textsw_insert (msgsw, prefix_blanks, strlen(prefix_blanks));
  217.         }
  218.         else
  219.             (void) textsw_insert (msgsw, prefix_blanks, strlen(prefix_blanks));
  220.         (void) textsw_insert (msgsw, msg_remain, strlen(msg_remain));
  221.         (void) textsw_insert (msgsw, "\n", 1);
  222.     }
  223.  
  224.     textsw_normalize_view (msgsw, msgsw_ipoint);
  225.     return;
  226. }
  227. /* EOF */
  228.  
  229.